/* 
 * File:   SBSImage.cpp
 * Author: paula
 * 
 * Created on 13 de septiembre de 2014, 17:05
 */

#include "SBSImage.h"

SBSImage::SBSImage() {
}

SBSImage::SBSImage(const SBSImage& orig) {

    this->myLogFile->getInstance(MY_LOG_MODE);
}

SBSImage::~SBSImage() {
    this->freeMedia();
}

bool SBSImage::loadMedia(string path, SDL_Renderer** gRenderer) {
    bool success = true;
    SDL_Surface* loadedSurface = IMG_Load(path.data());
    if (loadedSurface == NULL) {
        string lineToLog = "";
        lineToLog = "[2] No se pudo cargar el archivo: " + path + ". Error: " + IMG_GetError() + "\n";
        myLogFile->writeInLog(2, lineToLog);
        success = false;
    } else {
        this->texture = SDL_CreateTextureFromSurface(*gRenderer, loadedSurface);
        if (this->texture == NULL) {
            string lineToLog = "";
            lineToLog = "[2] No se pudo crear una textura en base al archivo: " + path + ". Error: " + IMG_GetError() + "\n";
            myLogFile->writeInLog(2, lineToLog);
            success = false;
        } else {
            string lineToLog = "";
            lineToLog = "[0] Se cargó exitosamente el archivo: " + path + "\n";
            myLogFile->writeInLog(0, lineToLog);
            this->width = loadedSurface->w;
            this->height = loadedSurface->h;
        }
        SDL_FreeSurface(loadedSurface);
    }
    return success;
}

void SBSImage::freeMedia() {
    SDL_DestroyTexture(this->texture);
    this->texture = NULL;
}

int SBSImage::getWidth() {
    return this->width;
}

int SBSImage::getHeight() {
    return this->height;
}

SDL_Texture* SBSImage::getTexture() {
    return this->texture;
}
